<?php
namespace app\admin\controller;

use app\common\model\Usergame as UsergameModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;
use think\Session;

class Usergame extends AdminBase
{
    protected $usergame_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->usergame_model = new UsergameModel();
    }

    /**
     * 管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($gameName = '',$userName = '',$userPhone = '', $page = 1)
    {
        $map = [];
        $select = [];
        if ($gameName) {
            $map['gameName'] = ['like', "%{$gameName}%"];
        }
        if($userName){
            $select['name'] = ['like',"{$userName}"];
        }
        if($userPhone){
            $select['phone'] = ['like',"{$userPhone}"];
        }

        if(!empty($select)){
            $user_id = Db('user')->where($select)->where('status',1)->find();
            $user_select_id = $user_id['id'];
            if($user_select_id){
                $map['userID'] = ['like',"{$user_select_id}"];
            }else{
                $map['userID'] = ['like',"0"];
            }
        }

        $user_list = Db('user')->where('status',1)->select();
        $usergame_list = $this->usergame_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('index', ['usergame_list' => $usergame_list, 'gameName' => $gameName,'user_list' => $user_list,'userName' => $userName,'userPhone' => $userPhone]);
    }

    /**
     * 编辑
     * @param $id
     * @return mixed
     */
    public function dvjd($id)
    {
        $usergame = $this->usergame_model->find($id);

        $user = Db('user')->where('id',$usergame['userID'])->where('status',1)->find();

        return $this->fetch('dvjd', ['usergame' => $usergame,'user' => $user]);
    }

    /**
     * 更新
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['disposeDate'] = date('Y-m-d H:i:s',time());
            $data['disposeMan']  = Session::get('admin_name');
            if ($this->usergame_model->allowField(true)->save($data, $id) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id = 0, $ids = [])
    {
        $id = $ids ? array_merge($ids) : $id;
        if ($id) {
            if ($this->usergame_model->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的内容');
        }
    }
    
}